<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <el-select
          v-model="query.status"
          placeholder="选择日期"
          clearable
          style="margin-right: 10px;"
        >
          <el-option
            v-for="item in dateList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>

        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button type="primary" @click="handleExport">导出</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="bno"
          align="center"
          label="磅单号"
        ></el-table-column>
        <el-table-column
          prop="customer"
          align="center"
          label="客户名称"
        ></el-table-column>
        <el-table-column
          prop="tradeday"
          align="center"
          label="交易日期"
        ></el-table-column>
        <el-table-column
          prop="intime"
          align="center"
          label="过毛时间"
        ></el-table-column>
        <el-table-column
          prop="outtime"
          align="center"
          label="过皮时间"
        ></el-table-column>

        <el-table-column
          prop="weightGross"
          align="center"
          label="毛重(kg)"
        ></el-table-column>

        <el-table-column
          prop="weightTare"
          align="center"
          label="皮重(kg)"
        ></el-table-column>
        <el-table-column
          prop="weightNet"
          align="center"
          label="净重(kg)"
        ></el-table-column>

        <el-table-column
          prop="productText"
          align="center"
          label="品类名称"
        ></el-table-column>

        <el-table-column
          prop="price"
          align="center"
          label="单价"
        ></el-table-column>

        <el-table-column
          prop="total"
          align="center"
          label="总额"
        ></el-table-column>

        <el-table-column
          prop="rater"
          align="center"
          label="定价员"
        ></el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import NProgress from "nprogress";
export default {
  name: "XintangSortingCenter",
  data() {
    return {
      dateList: [
        {
          value: "1",
          label: "昨日",
        },
        {
          value: "2",
          label: "上周",
        },
        {
          value: "3",
          label: "本月",
        },
      ],
      query: {
        pageIndex: 1,
        pageSize: 20,
        status: "",
      },
      value2: "",
      tableData: [],
      delList: [],
      pageTotal: 0,
      form: {},
      idx: -1,
      id: -1,
    };
  },
  watch: {
    "query.status"(newVal, oldVal) {
      if (newVal != "") {
        this.value2 = "";
      }
    },
    value2(newVal, oldVal) {
      if (newVal != "") {
        this.query.status = "";
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    //导出
    handleExport() {
      NProgress.start();
      this.$axios
        .get("/papercollection/export", {
          params: {
            startDate: this.value2 ? this.value2[0].getTime().toString() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
            status: this.query.status,
            organCode: 4,
            code: 1
          },
          responseType: "blob",
        })
        .then((res) => {
          console.log(res);
          if (res.data.type != "application/vnd.ms-excel") {
            NProgress.done();
            return this.$message.warning({
              message: res.msg,
              duration: 1500,
            });
          }

          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);

          NProgress.done();
        })
        .catch((err) => {});
    },
    // 获取数据
    getData() {
      this.$axios
        .get("/papercollection/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            startDate: this.value2 ? this.value2[0].getTime().toString() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
            status: this.query.status,
            organCode: 4,
            code: 1
          },
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
        })
        .catch((err) => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },

    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
